package com.yuyou.fn.wage.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.TeacherQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.platform.service.ISysUserRoleService;
import com.yuyou.fn.wage.entity.WageCoefficientPerformance;
import com.yuyou.fn.wage.entity.WageCoefficientPerformanceHistory;
import com.yuyou.fn.wage.mapper.WageCoefficientPerformanceMapper;
import com.yuyou.fn.wage.service.IWageCoefficientPerformanceHistoryService;
import com.yuyou.fn.wage.service.IWageCoefficientPerformanceService;
import com.yuyou.fn.wage.vo.CoefficientPerformanceUpdateVo;
import com.yuyou.fn.wage.vo.EmployeeCoefficientPerformance;
import com.yuyou.fn.wage.vo.ImportTeacherCoefficientPerformanceVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * WageCoefficientPerformance 表数据服务层接口实现类
 *
 */
@Service
public class WageCoefficientPerformanceServiceImpl extends SuperServiceImpl<WageCoefficientPerformanceMapper, WageCoefficientPerformance,Long> implements IWageCoefficientPerformanceService {
    @Resource
    private ITeacherService teacherService;

    @Resource
    private IWageCoefficientPerformanceHistoryService wageCoefficientPerformanceHistoryService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Override
    public Page<EmployeeCoefficientPerformance> findTeacherCoefficientPerformancePage(DataPermission dataPermission, TeacherQueryVo teacherQueryVo, int pageNo, int pageSize) {
        Page<Teacher> teacherPage=teacherService.findTeacherByQueryForPage(teacherQueryVo,dataPermission,pageNo,pageSize);
        List<Teacher> teacherList=teacherPage.getRecords();
        Page<EmployeeCoefficientPerformance> page= Utils.newPage(teacherPage);
        if (teacherList!=null){
            List<Long> teacherIds= BeanUtils.toList(teacherList,"teacherId");
            List<EmployeeCoefficientPerformance> employeeCoefficientPerformanceList =new ArrayList<>(teacherIds.size());
            List<WageCoefficientPerformance> wageCoefficientPerformanceList=this.findCoefficientPerformanceByEmployeeIds(teacherIds);
            Map<Long,WageCoefficientPerformance> map=BeanUtils.toMap(wageCoefficientPerformanceList,"employeeId");
            for (Teacher teacher:teacherList){
                EmployeeCoefficientPerformance employeeCoefficientPerformance =new EmployeeCoefficientPerformance();
                employeeCoefficientPerformance.setEmployeeId(teacher.getTeacherId());
                employeeCoefficientPerformance.setEmployeeName(teacher.getTeacherName());
                employeeCoefficientPerformance.setIdNo(teacher.getIdNo());
                employeeCoefficientPerformance.setStatus(teacher.getStatus());
                employeeCoefficientPerformance.setBusTeamId(teacher.getBusTeamId());
                employeeCoefficientPerformance.setBusTeamName(teacher.getBusTeamName());
                WageCoefficientPerformance wageCoefficientPerformance =map.get(teacher.getTeacherId());
                if (wageCoefficientPerformance !=null){
                    BeanUtils.copyProperties(employeeCoefficientPerformance, wageCoefficientPerformance);
                }
                employeeCoefficientPerformance.setType(1);
                employeeCoefficientPerformanceList.add(employeeCoefficientPerformance);
            }
            page.setRecords(employeeCoefficientPerformanceList);
        }

        return page;
    }

    @Override
    public SXSSFWorkbook exportTeacherCoefficientPerformance(DataPermission dataPermission, TeacherQueryVo teacherQueryVo) {
        Page<EmployeeCoefficientPerformance> teacherCoefficientPerformancePage = this.findTeacherCoefficientPerformancePage(dataPermission, teacherQueryVo, 1, 20000);

        return exportTeacherCoefficientPerformance(teacherCoefficientPerformancePage.getRecords(),sysUserRoleService.showIdNoByCheckRoleNames(Principals.get().id));
    }

    public static SXSSFWorkbook exportTeacherCoefficientPerformance(List<EmployeeCoefficientPerformance> employeeCoefficientPerformances,Boolean showIdNo) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("绩效系数");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"姓名","身份证","人事单位","在职状态","绩效系数"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        for (EmployeeCoefficientPerformance employeeCoefficientPerformance:employeeCoefficientPerformances){
            int cellNum = 0;
            SXSSFRow row = sheet.createRow(rowNum++);

            SXSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(employeeCoefficientPerformance.getEmployeeName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(showIdNo?employeeCoefficientPerformance.getIdNo():""));

     /*       cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(employeeCoefficientPerformance.getIdNo()));*/

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(employeeCoefficientPerformance.getBusTeamName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(employeeCoefficientPerformance.getStatus()==null?"":employeeCoefficientPerformance.getStatus()==1?"在职":"离职");

            cell = row.createCell(cellNum++);
            cell.setCellValue(employeeCoefficientPerformance.getCoefficientPerformance());


        }
        return excel;
    }



    @Override
    public EmployeeCoefficientPerformance findTeacherCoefficientPerformance(Long teacherId) {
        WageCoefficientPerformance wageCoefficientPerformance=this.findById(teacherId);
        Teacher teacher=teacherService.findById(teacherId);
        EmployeeCoefficientPerformance employeeCoefficientPerformance =new EmployeeCoefficientPerformance();
        employeeCoefficientPerformance.setEmployeeId(teacher.getTeacherId());
        employeeCoefficientPerformance.setEmployeeName(teacher.getTeacherName());
        employeeCoefficientPerformance.setIdNo(teacher.getIdNo());
        if (wageCoefficientPerformance !=null){
            BeanUtils.copyProperties(employeeCoefficientPerformance, wageCoefficientPerformance);
        }
        return employeeCoefficientPerformance;
    }

    @Override
    public List<WageCoefficientPerformance> findCoefficientPerformanceByEmployeeIds(List<Long> employeeIds) {
        if (employeeIds==null || employeeIds.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example<WageCoefficientPerformance> example= WageCoefficientPerformance.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("employeeId",employeeIds);
        return this.findListByExample(example);
    }

    @Override
    @Transactional
    public void updateEmployeeCoefficientPerformanceSetting(CoefficientPerformanceUpdateVo coefficientPerformanceUpdateVo) {
        if (coefficientPerformanceUpdateVo.getEmployeeId()==null){
            throw new BusException("请传入id");
        }
        if (coefficientPerformanceUpdateVo.getType()==null){
            throw new BusException("缺乏员工类型");
        }
        if (coefficientPerformanceUpdateVo.getCoefficientPerformance()==null){
            throw new BusException("缺乏填写系数");
        }

        WageCoefficientPerformance wageCoefficientPerformance =new WageCoefficientPerformance();
        BeanUtils.copyProperties(wageCoefficientPerformance, coefficientPerformanceUpdateVo);
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("查不到操作人");
        }
        wageCoefficientPerformance.setOperatorId(principal.id);
        wageCoefficientPerformance.setOperatorName(principal.name);
        wageCoefficientPerformance.setUpdateTime(TimeUtils.now());
        WageCoefficientPerformance check=this.findById(coefficientPerformanceUpdateVo.getEmployeeId());
        WageCoefficientPerformanceHistory wageCoefficientPerformanceHistory=new WageCoefficientPerformanceHistory();
        if (check!=null){
            this.updateSelectiveById(wageCoefficientPerformance);
        }else {
            wageCoefficientPerformance.setCreateTime(TimeUtils.now());
            this.insert(wageCoefficientPerformance);
        }
        BeanUtils.copyProperties(wageCoefficientPerformanceHistory,wageCoefficientPerformance);
        wageCoefficientPerformanceHistory.setCreateTime(TimeUtils.now());
        wageCoefficientPerformanceHistoryService.insert(wageCoefficientPerformanceHistory);



    }

    @Override
    @Transactional
    public void importTeacherCoefficientPerformanceSetting(List<ImportTeacherCoefficientPerformanceVo> importTeacherCoefficientPerformanceVos) {
        if (importTeacherCoefficientPerformanceVos ==null || importTeacherCoefficientPerformanceVos.size()<=0){
            throw new BusException("无信息导入");
        }
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("请登录后操作");
        }
        List<String> idNos=BeanUtils.toList(importTeacherCoefficientPerformanceVos,"idNo");
        List<Teacher> teacherList=teacherService.findTeacherListByIdNos(idNos);
        List<Long> teacherIdList=BeanUtils.toList(teacherList,"teacherId");
        if (teacherIdList==null || teacherIdList.size()<=0){
            throw new BusException("查不到表格里的老师");
        }
        List<WageCoefficientPerformance> wageCoefficientPerformanceList=this.findCoefficientPerformanceByEmployeeIds(teacherIdList);
        /*记录已薪酬的teacherId*/
        List<Long> haveTeacherIds=BeanUtils.toList(wageCoefficientPerformanceList,"employeeId");
        List<WageCoefficientPerformance> insertList=new ArrayList<>(teacherIdList.size());

        Map<Long,Teacher> teacherMap=BeanUtils.toMap(teacherList,"idNo");
        int i=2;
        for (ImportTeacherCoefficientPerformanceVo importTeacherCoefficientPerformanceVo : importTeacherCoefficientPerformanceVos){
            if (importTeacherCoefficientPerformanceVo.getIdNo()==null || importTeacherCoefficientPerformanceVo.getCoefficientPerformance()==null || importTeacherCoefficientPerformanceVo.getIdNo()==null  ){
                throw new BusException("第"+i+"行数据有空字段请检查");
            }
            Teacher teacher=teacherMap.get(importTeacherCoefficientPerformanceVo.getIdNo());
            if (teacher==null){
                throw new BusException("第"+i+"行数据查不到拥有该身份证的老师");
            }

            if (haveTeacherIds.contains(teacher.getTeacherId())){
                CoefficientPerformanceUpdateVo employeeCoefficientPerformance =new CoefficientPerformanceUpdateVo();
                employeeCoefficientPerformance.setEmployeeId(teacher.getTeacherId());

                BeanUtils.copyProperties(employeeCoefficientPerformance, importTeacherCoefficientPerformanceVo);
                this.updateEmployeeCoefficientPerformanceSetting(employeeCoefficientPerformance);
            }else {
                WageCoefficientPerformance wageCoefficientPerformance =new WageCoefficientPerformance();
                wageCoefficientPerformance.setEmployeeId(teacher.getTeacherId());
                BeanUtils.copyProperties(wageCoefficientPerformance, importTeacherCoefficientPerformanceVo);
                wageCoefficientPerformance.setUpdateTime(TimeUtils.now());
                wageCoefficientPerformance.setOperatorId(principal.id);
                wageCoefficientPerformance.setOperatorName(principal.name);
                insertList.add(wageCoefficientPerformance);
            }
        }
        if (insertList.size()>0){
            this.insertBatch(insertList);
        }
    }
}